package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.domain.dto.req.FileDTO;
import com.winshare.saleAssistant.domain.dto.req.FileDeleteDTO;
import com.winshare.saleAssistant.service.FileService;
import com.winshare.saleAssistant.util.Encodes;
import org.springframework.stereotype.Service;

@Service
public class FileServiceImpl implements FileService {

    private final OssService ossService;

    public FileServiceImpl(OssService ossService) {
        this.ossService = ossService;
    }

    @Override
    public String upload(FileDTO fileDTO) {
        String content =fileDTO.getFileContent();
        byte[] bytes = Encodes.decodeBase64(content);
        return ossService.upload(fileDTO.getFileName(), bytes);
    }

    @Override
    public void delete(FileDeleteDTO fileDeleteDTO) {
        String url = fileDeleteDTO.getFileUrl().split("//")[1];
        int position = url.indexOf("/");
        String fileName = url.substring(position + 1);
        ossService.delete(fileName);
    }
}
